//
//  D_2ViewController.m
//  RACExamale
//
//  Created by cym_bj on 2017/12/1.
//  Copyright © 2017年 com.cym. All rights reserved.
//

#import "D_2ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface D_2ViewController ()

@end

@implementation D_2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"RACMulticastConnection";

    
    
    [self createSignal];
    [self createSubject];
    [self createConnection];
    
    
    
    
}
-(void)createSignal
{
    RACSignal *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        [subscriber sendNext:@"发送数据"];
        return nil;
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"订阅1=%@",x);
        
    }];
    
    [signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"订阅2=%@",x);
        
    }];
}
-(void)createSubject
{
    RACSubject *subject=[RACSubject subject];
    [subject subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"订阅一:%@",x);
        
    }];
    
    [subject subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"订阅二:%@",x);
    }];
    
    
    [subject sendNext:@"发送哈哈"];
}

#pragma mark -连接类
-(void)createConnection
{
    /*
     
     RACMulticastConnection : 用于当一个信号，被多次订阅时，为了保证创建信号时，避免多次调用创建信号中的block，造成副作用，可以使用这个类处理
     
     使用注意:RACMulticastConnection通过RACSignal的-publish或者-muticast:方法创建.
     
     
     */
    
    RACSignal *signal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        NSLog(@"创建信号");
        [subscriber sendNext:@"发送信号"];
        return nil;
    }];
    
    
    //把信号转换成连接类
    RACMulticastConnection *connection=[signal multicast:[RACSubject subject]];
    
    //订阅连接类信号
    [connection.signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@",x);
    }];
    
    //订阅连接类信号
    [connection.signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@",x);
    }];
    
    //连接
    [connection connect];
}




@end
